/*
    Android UIML

	Copyright (C) 2010  Bram Goffings (bramgoffings@gmail.com)

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU Lesser General Public License
	as published by the Free Software Foundation; either version 2.1
	of	the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Lesser General Public License for more details.

	You should have received a copy of the GNU Lesser General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

package uiml.android.template;

import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;

import uiml.android.elements.Behavior;
import uiml.android.elements.Constant;
import uiml.android.elements.Content;
import uiml.android.elements.Interface;
import uiml.android.elements.Logic;
import uiml.android.elements.Part;
import uiml.android.elements.Property;
import uiml.android.elements.Structure;
import uiml.android.elements.Style;
import uiml.android.elements.UimlElement;
import uiml.android.elements.UimlElementWithAttributes;
import uiml.android.elements.peers.DClass;
import uiml.android.elements.peers.DComponent;
import uiml.android.elements.peers.Peer;
import uiml.android.elements.peers.Presentation;
import uiml.android.elements.peers.Script;

public class Template extends UimlElement{
	
	// TODO comment
	UimlElement top = null;
	
	// Attributen
	String id = null;

	// TODO comment
	public final String ID = "id";
	public static String IAM = "template";
	
	/**
	 * 
	 * @param node
	 */
	public Template(Element node){
		super(node);
		process();
	}

	/**
	 * 
	 */
	public void process() {
		processAttributes();
		super.process();
		
		// Voeg Template toe aan onze template repository
		TemplateRepository.getInstance().register(this);
	}
	
	/**
	 * 
	 */
	private void processAttributes() {
		
		NamedNodeMap attributes = node.getAttributes();
		
		if(attributes != null) {
			if(attributes.getNamedItem(ID) != null)
				setId(attributes.getNamedItem(ID).getNodeValue());
		}
	}

	/**
	 * 
	 */
	@Override
	public void processElement(Element elem) {

		if(elem.getTagName().equals(Behavior.IAM))
			top = new Behavior(elem);
		else if(elem.getTagName().equals(DClass.IAM))
			top = new DClass(elem);
		else if(elem.getTagName().equals(DComponent.IAM))
			top = new DComponent(elem);
		else if(elem.getTagName().equals(Constant.IAM))
			top = new Constant(elem);
		else if(elem.getTagName().equals(Content.IAM))
			top = new Content(elem);
		else if(elem.getTagName().equals(Interface.IAM))
			top = new Interface(elem);
		else if(elem.getTagName().equals(Logic.IAM))
			top = new Logic(elem);
		else if(elem.getTagName().equals(Part.IAM))
			top = new Part(elem);
		else if(elem.getTagName().equals(Peer.IAM))
			top = new Peer(elem);
		else if(elem.getTagName().equals(Presentation.IAM))
			top = new Presentation(elem);
		else if(elem.getTagName().equals(Property.IAM))
			top = new Property(elem);
		else if(elem.getTagName().equals(Script.IAM))
			top = new Script(elem);	
		else if(elem.getTagName().equals(Structure.IAM))
			top = new Structure(elem);	
		if(elem.getTagName().equals(Style.IAM))
			top = new Style(elem);
	}
	
	/**
	 * 
	 * @param how
	 * @return
	 */
	public TemplateResolver getTemplateResolver(String how) {
		
		if(how.equals(UimlElementWithAttributes.CASCADE))
			return new CascadeTemplateResolver();
		else
			return null;
	}

	/**
	 * @return the top
	 */
	public UimlElement getTop() {
		return top;
	}

	/**
	 * @param top the top to set
	 */
	public void setTop(UimlElement top) {
		this.top = top;
	}

	/**
	 * @return the id
	 */
	public String getId() {
		return id;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(String id) {
		this.id = id;
	}

}
